package sunwin.yoms.dao.test;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.testng.annotations.Test;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 田西泽 on 2015-8-27.
 */
public class BaseDataHelper {
    @Test
    public void main() throws IOException {
        setSysFuncValueToBinary(32, 96);
        //setSysFuncValueToZero(32, 96);
    }

    /**
     * 设置功能value为二进制
     * @param count
     * @param startId
     * @throws IOException
     */
    public void setSysFuncValueToBinary(int count, int startId) throws IOException {
        SqlSession session = this.getSqlSession();
        long j = 1;
        for (int i = startId; i <= startId + count; i++) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("id", i);
            params.put("value", j);
            session.update("myUpdate", params);
            j = j * 2;
        }
        session.commit();
        session.close();
    }

    /**
     * 设置功能value为0
     *
     * @param count
     * @throws IOException
     */
    public void setSysFuncValueToZero(int count, int startId) throws IOException {
        SqlSession session = this.getSqlSession();
        for (int i = startId; i <= count + startId; i++) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("id", i);
            params.put("value", 0);
            session.update("myUpdate", params);
        }
        session.commit();
        session.close();
    }

    private SqlSession getSqlSession() throws IOException {
        Reader reader = Resources.getResourceAsReader("mybatis.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = ssf.openSession();

        return session;
    }
}
